// pages/createAccountBook/createAccountBook.js
Page({
  data: {
    types: [
      {id: 1, name: '日常开销',icon:'/miniprogram/images/book/rc.jpg'},
      {id: 2, name: '旅行记录',icon:'/miniprogram/images/book/lx.jpg'}, 
      {id: 3, name: '恋爱基金',icon:'/miniprogram/images/book/jt.jpg'},
      {id: 4, name: '学习投资',icon:'/miniprogram/images/book/xy.jpg'},
      {id: 5, name: '其他',icon:'/miniprogram/images/book/zx.jpg'}
    ],
    currentType: 0,
    bookName: ''
  },

  typeChange(e) {
    this.setData({
      currentType: e.detail.value
    })
  },

  nameInput(e) {
    this.setData({
      bookName: e.detail.value
    })
  },

  createBook() {
    if (!this.data.bookName) {
      wx.showToast({
        title: '请输入账本名称',
        icon: 'none'
      })
      return
    }
    const that = this
    wx.cloud.callFunction({
      name: 'saveAccountBook',
      data: {
        typeName: this.data.types[this.data.currentType],
        name: this.data.bookName,
        openid: getApp().globalData.openid
      },
      success(res) {
        if (res.result.code === 200) {
          wx.showToast({ title: '创建成功' })
          wx.navigateBack({detail:1})
        } else {
          wx.showToast({ title: res.result.msg, icon: 'none' })
        }
      },
      fail(err) {
        wx.showToast({ title: '网络错误', icon: 'none' })
      }
    })
  }
})